<%
	local util = require "luci.util"
	local fs = require "nixio.fs"
	local sys = require "luci.sys"
	local http = require "luci.http"
	local dispatcher = require "luci.dispatcher"
	local uci = require "luci.model.uci".cursor()

-%>


<%+header%>

<!--
-- Copyright 2020-2022 Rafał Wabik (IceG) - From eko.one.pl forum
-- Licensed to the GNU General Public License v3.0.
-->

	<h2><%:USSD Codes%></h2>
	<div class="cbi-map-descr"><%:Web UI for handling USSD codes via sms_tool. More information about the sms-tool on the%> <a href="https://eko.one.pl/?p=openwrt-sms_tool" target="_blank"><%:eko.one.pl forum%></a>.</div>
	<p></p>
	<h4><%:Sending USSD codes%></h4>
	<div class="table" width="100%">

		<div class="tr">
			<div class="td left" width="25%"><%:User USSD Codes%>:</div>

			<div class="td left" >
				<select name="ussd" id="pl" onclick="copyFunction()">

 				</select>
			</div>
			<div class="td left" ></div>
			</div>

		<div class="tr">
			<div class="td left" ><%:Code to send%>:</div>
			<div class="td left" ><input type="text" id="code" required size="20"></div>

		</div>
		<div class="tr">
			<div class="td left" ><%:Reply%>:</div><div class="td left" ><pre id="odp" style="visibility: hidden;"></pre></div>

		</div>
	</div>

	<div class="table" width="100%">
		<div class="tr cbi-rowstyle-2">
		<div class="td right"><input type="button" style="margin-right: 5%"; id="sendussd" class="btn cbi-button cbi-button-neutral" value="<%:Send Code%>" /></div>
		</div>

	</div>

<script type="text/javascript">//<![CDATA[


window.onload = function readUSER() {


			XHR.get('<%=luci.dispatcher.build_url("admin", "modem", "sms", "user_ussd")%>', null,
			function(x, json)
			{
				select = document.getElementById('pl');

				var count = Object.keys(json).length;


					for(var d=0;d<=count;d++)
						{
    			    				var opt = document.createElement('option');
							var s = json[d].usd;
							var fields = s.split(/;/);
							var name = fields[0];
							var code = fields[1];
							opt.text = name;
							opt.value = code.trim();
    					    		opt.innerHTML = name;
    							select.appendChild(opt);
						}

			}
			);



}

function copyFunction() {
  var node = document.getElementById('odp');
  node.style.visibility = 'hidden';
  
  var x = document.getElementById("pl").value;
  document.getElementById("code").value = x;
  document.getElementById("odp").innerHTML = "";
}

function postcmd(cmd) {
    (new XHR()).post("<%=luci.dispatcher.build_url("admin", "modem", "sms", "run_ussd")%>", {"code":cmd}, function(x) {
        console.log(x.response)
        console.log(x)
	 	var cut = x.response;
		if ( true == cut.includes("error: 0") ) {
        		document.getElementById("odp").innerHTML = "<%:Phone/Modem failure.%>"};
		if ( true == cut.includes("error: 1") ) {
        		document.getElementById("odp").innerHTML = "<%:No connection to phone.%>"};
		if ( true == cut.includes("error: 2") ) {
        		document.getElementById("odp").innerHTML = "<%:Phone/Modem adapter link reserved.%>"};
		if ( true == cut.includes("error: 3") ) {
        		document.getElementById("odp").innerHTML = "<%:Operation not allowed.%>"};
		if ( true == cut.includes("error: 4") ) {
        		document.getElementById("odp").innerHTML = "<%:Operation not supported.%>"};
		if ( true == cut.includes("error: 5") ) {
        		document.getElementById("odp").innerHTML = "<%:PH_SIM PIN required.%>"};
		if ( true == cut.includes("error: 6") ) {
        		document.getElementById("odp").innerHTML = "<%:PH_FSIM PIN required.%>"};
		if ( true == cut.includes("error: 7") ) {
        		document.getElementById("odp").innerHTML = "<%:PH_FSIM PUK required.%>"};
		if ( true == cut.includes("error: 10") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM not inserted.%>"};
		if ( true == cut.includes("error: 11") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM PIN required.%>"};
		if ( true == cut.includes("error: 12") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM PUK required.%>"};
		if ( true == cut.includes("error: 13") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM failure.%>"};
		if ( true == cut.includes("error: 14") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM busy.%>"};
		if ( true == cut.includes("error: 15") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM wrong.%>"};
		if ( true == cut.includes("error: 16") ) {
        		document.getElementById("odp").innerHTML = "<%:Incorrect password.%>"};
		if ( true == cut.includes("error: 17") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM PIN2 required.%>"};
		if ( true == cut.includes("error: 18") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM PUK2 required.%>"};
		if ( true == cut.includes("error: 20") ) {
        		document.getElementById("odp").innerHTML = "<%:Memory full.%>"};
		if ( true == cut.includes("error: 21") ) {
        		document.getElementById("odp").innerHTML = "<%:Invalid index.%>"};
		if ( true == cut.includes("error: 22") ) {
        		document.getElementById("odp").innerHTML = "<%:Not found.%>"};
		if ( true == cut.includes("error: 23") ) {
        		document.getElementById("odp").innerHTML = "<%:Memory failure.%>"};
		if ( true == cut.includes("error: 24") ) {
        		document.getElementById("odp").innerHTML = "<%:Text string too long.%>"};
		if ( true == cut.includes("error: 25") ) {
        		document.getElementById("odp").innerHTML = "<%:Invalid characters in text string.%>"};
		if ( true == cut.includes("error: 26") ) {
        		document.getElementById("odp").innerHTML = "<%:Dial string too long.%>"};
		if ( true == cut.includes("error: 27") ) {
        		document.getElementById("odp").innerHTML = "<%:Invalid characters in dial string.%>"};
		if ( true == cut.includes("error: 30") ) {
        		document.getElementById("odp").innerHTML = "<%:No network service.%>"};
		if ( true == cut.includes("error: 31") ) {
        		document.getElementById("odp").innerHTML = "<%:Network timeout.%>"};
		if ( true == cut.includes("error: 32") ) {
        		document.getElementById("odp").innerHTML = "<%:Network not allowed, emergency calls only.%>"};
		if ( true == cut.includes("error: 40") ) {
        		document.getElementById("odp").innerHTML = "<%:Network personalization PIN required.%>"};
		if ( true == cut.includes("error: 41") ) {
        		document.getElementById("odp").innerHTML = "<%:Network personalization PUK required.%>"};
		if ( true == cut.includes("error: 42") ) {
        		document.getElementById("odp").innerHTML = "<%:Network subset personalization PIN required.%>"};
		if ( true == cut.includes("error: 43") ) {
        		document.getElementById("odp").innerHTML = "<%:Network subset personalization PUK required.%>"};
		if ( true == cut.includes("error: 44") ) {
        		document.getElementById("odp").innerHTML = "<%:Service provider personalization PIN required.%>"};
		if ( true == cut.includes("error: 45") ) {
        		document.getElementById("odp").innerHTML = "<%:Service provider personalization PUK required.%>"};
		if ( true == cut.includes("error: 46") ) {
        		document.getElementById("odp").innerHTML = "<%:Corporate personalization PIN required.%>"};
		if ( true == cut.includes("error: 47") ) {
        		document.getElementById("odp").innerHTML = "<%:Corporate personalization PUK required.%>"};
		if ( true == cut.includes("error: 48") ) {
        		document.getElementById("odp").innerHTML = "<%:PH-SIM PUK required.%>"};
		if ( true == cut.includes("error: 100") ) {
        		document.getElementById("odp").innerHTML = "<%:Unknown error.%>"};
		if ( true == cut.includes("error: 103") ) {
        		document.getElementById("odp").innerHTML = "<%:Illegal MS.%>"};
		if ( true == cut.includes("error: 106") ) {
        		document.getElementById("odp").innerHTML = "<%:Illegal ME.%>"};
		if ( true == cut.includes("error: 107") ) {
        		document.getElementById("odp").innerHTML = "<%:GPRS services not allowed.%>"};
		if ( true == cut.includes("error: 111") ) {
        		document.getElementById("odp").innerHTML = "<%:PLMN not allowed.%>"};
		if ( true == cut.includes("error: 112") ) {
        		document.getElementById("odp").innerHTML = "<%:Location area not allowed.%>"};
		if ( true == cut.includes("error: 113") ) {
        		document.getElementById("odp").innerHTML = "<%:Roaming not allowed in this location area.%>"};
		if ( true == cut.includes("error: 126") ) {
        		document.getElementById("odp").innerHTML = "<%:Operation temporary not allowed.%>"};
		if ( true == cut.includes("error: 132") ) {
        		document.getElementById("odp").innerHTML = "<%:Service operation not supported.%>"};
		if ( true == cut.includes("error: 133") ) {
        		document.getElementById("odp").innerHTML = "<%:Requested service option not subscribed.%>"};
		if ( true == cut.includes("error: 134") ) {
        		document.getElementById("odp").innerHTML = "<%:Service option temporary out of order.%>"};
		if ( true == cut.includes("error: 148") ) {
        		document.getElementById("odp").innerHTML = "<%:Unspecified GPRS error.%>"};
		if ( true == cut.includes("error: 149") ) {
        		document.getElementById("odp").innerHTML = "<%:PDP authentication failure.%>"};
		if ( true == cut.includes("error: 150") ) {
        		document.getElementById("odp").innerHTML = "<%:Invalid mobile class.%>"};
		if ( true == cut.includes("error: 256") ) {
        		document.getElementById("odp").innerHTML = "<%:Operation temporarily not allowed.%>"};
		if ( true == cut.includes("error: 257") ) {
        		document.getElementById("odp").innerHTML = "<%:Call barred.%>"};
		if ( true == cut.includes("error: 258") ) {
        		document.getElementById("odp").innerHTML = "<%:Phone/Modem is busy.%>"};
		if ( true == cut.includes("error: 259") ) {
        		document.getElementById("odp").innerHTML = "<%:User abort.%>"};
		if ( true == cut.includes("error: 260") ) {
        		document.getElementById("odp").innerHTML = "<%:Invalid dial string.%>"};
		if ( true == cut.includes("error: 261") ) {
        		document.getElementById("odp").innerHTML = "<%:SS not executed.%>"};
		if ( true == cut.includes("error: 262") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM Blocked.%>"};
		if ( true == cut.includes("error: 263") ) {
        		document.getElementById("odp").innerHTML = "<%:Invalid block.%>"};
		if ( true == cut.includes("error: 527") ) {
        		document.getElementById("odp").innerHTML = "<%:Please wait, and retry your selection later (Specific Modem Sierra).%>"};
		if ( true == cut.includes("error: 528") ) {
        		document.getElementById("odp").innerHTML = "<%:Location update failure – emergency calls only (Specific Modem Sierra).%>"};
		if ( true == cut.includes("error: 529") ) {
        		document.getElementById("odp").innerHTML = "<%:Selection failure – emergency calls only (Specific Modem Sierra).%>"};
		if ( true == cut.includes("error: 772") ) {
        		document.getElementById("odp").innerHTML = "<%:SIM powered down.%>"};
		if ( true != cut.includes("error: ") ) {
        		document.getElementById("odp").innerHTML = x.response};

    });
    return false;
}

document.addEventListener('DOMContentLoaded', function (ev) {var button = document.getElementById("sendussd");
        button.addEventListener("click", function () {


		var s = document.getElementById("code").value;
		if ( s.length == 0 )
		{
  			document.getElementById("odp").innerHTML = "";
			alert("<%:Please enter a USSD code%>");
			return false;
		}

        var cmd = document.getElementById("code");
        postcmd(cmd.value);
        cmd.value = "";

        var node = document.getElementById('odp');
    	if (node.style.visibility=='visible') {
        node.style.visibility = 'hidden';
    	}
    	else
        node.style.visibility = 'visible'

        return true;
        });
    }, true);


//]]></script>

<%+footer%>
